#!/usr/bin/env python
#-*- coding: utf-8 -*-
#__author__:vincentlc
#time: 16/5/28 : 09:46
import time
'''
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?
'''
def exeTime(func):
    def newFunc(*args, **args2):
        t0 = time.time()
        print ("@%s, {%s} start" % (time.strftime("%X", time.localtime()), func.__name__))
        back = func(*args, **args2)
        print ("@%s, {%s} end" % (time.strftime("%X", time.localtime()), func.__name__))
        print ("@%.3fs taken for {%s}" % (time.time() - t0, func.__name__))
        return back
    return newFunc

class Solution(object):
    @exeTime
    def isPowerOfTwo(self, num):
        """
        :type num: int
        :rtype: bool
        """
        # # t_begin = time.time()
        result =  (num!=0) and ((num&(num-1))==0)
        # print (time.time()-t_begin)
        return result

    @exeTime
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        # t_begin = time.time()
        while num != 0:
            if num == 1:
                return True
            # if ((int(num) & 1)==1):
            if ((int(num) & 1)==1):
                return False
            # num = num/4
            num = num//4

        # print (time.time()-t_begin)
        return False

    @exeTime
    def isPowerOfFour2(self, num):
        while num != 0:
            if num == 1:
                return True
            if (num%4)!=0:
                return False
            num = num//4

        return False

test_a = Solution()
# print("\nPower Of Two")
# print(test_a.isPowerOfTwo(4))
# print(test_a.isPowerOfTwo(16))
# print(test_a.isPowerOfTwo(15))
# print(test_a.isPowerOfTwo(28561))

print("\n\nPower Of Four")
# print(test_a.isPowerOfFour(4))
# print(test_a.isPowerOfFour(16))
# print(test_a.isPowerOfFour(15))
# print(test_a.isPowerOfFour(28561))
# print(test_a.isPowerOfFour(81))
# print(test_a.isPowerOfFour(1024))
print(test_a.isPowerOfFour2(1024))
print(test_a.isPowerOfFour2(6))

